﻿@{
    ViewBag.Title = "_AddUser";
}
@using Resources
@model DHQGHN.PM0.Models.Admin.UserModel

<link href="~/Content/jquery.treeview.css" rel="stylesheet" />
<link href="~/Content/gpcp-department.css" rel="stylesheet" />
<script src="~/Scripts/jquery.cookie.js"></script>
<script src="~/Scripts/jquery.treeview.js"></script>
<script src="~/Scripts/gpcp-department.js"></script>
<script src="~/Scripts/bootstrap.js"></script>
@*<script src="~/Scripts/jquery/jquery-1.3.2.js" type="text/javascript"></script>*@
@*<script src="~/Scripts/jquery/password-strength-meter.js" type="text/javascript"></script>*@

<script type="text/javascript"></script>
<style>
    .Validation_Error {
        margin-left: 30px;
    }

    .TopDivs {
        clear: both;
        vertical-align: top;
        text-align: left;
        margin-bottom: 5px;
        font-family: Arial, Verdana, Tahoma, sans-serif;
        font-size: 12px;
        font-weight: 100;
    }

    .modal-title {
        margin: 0;
        font-family: Arial, Verdana, Tahoma, sans-serif;
        font-size: 12px;
        font-weight: bold;
    }

    .modal-header {
        padding: 5px 10px 10px;
    }

    .modal-footer {
        padding: 5px 10px 10px;
        margin-top: 5px;
        text-align: center;
        border-top: 1px solid #e5e5e5;
    }

    .modal-body {
        position: relative;
        padding: 5px 10px 0px 10px;
    }

    .form-group {
        margin-bottom: 5px;
    }

    .modal-dialog {
        width: 430px;
    }

    .department_dialog {
        display: none;
        position: fixed;
        /*width: 380px;
            height: 200px;*/
        /*top: 50%;
            left: 50%;*/
        margin-left: -190px;
        margin-top: -100px;
        background-color: #ffffff;
        border: 1px solid #C4C4C4; /*1px solid rgba(0, 0, 0, 0.2); 1px solid #336699;*/
        padding: 0px;
        box-shadow: 0 0 8px #D9D9D9;
        z-index: 1060 !important;
        font-family: Verdana;
        font-size: 10pt;
        border-radius: 6px;
    }

    .department_hearder {
        float: left;
        width: 100%;
        background: #e5e5e5;
        padding-left: 5px;
        border-top-left-radius: 6px;
        border-top-right-radius: 6px;
        height: 25px;
    }

    .department_body {
        float: left;
        overflow-x: hidden;
        padding-left: 5px;
        overflow-y: auto;
        height: auto;
        max-height: 200px;
        width: 100%;
    }
</style>
@Html.ValidationSummary(false)
@using (
                Html.BeginForm(
                    actionName: "actionCreateOrAddUser", controllerName: "Users", routeValues: new { }, method: FormMethod.Post, htmlAttributes: new { @id = "user-form", @class = "form-inline", @role = "form", @style = "width: 100%;" }
                    )
                )
{
    <div class="modal-body min-width">
        <div class="form-group">
            @Html.AntiForgeryToken()
            @Html.HiddenFor(m => m.ID)            
            
            <div style="margin-top:4px;">
                @Html.LabelFor(m => m.FullName, new { @class = "labels", style = "width:100px;" })<span class="field-validation-error"> * </span>
                @Html.TextBoxFor(m => m.FullName, new { @autocomplete = "off", @class = "TextBoxContent", style = "width:200px", @placeholder = "" })
                @Html.ValidationMessageFor(m => m.FullName, null, new { @class = "Validation_Error" })
            </div>
            @if ((Model != null) && (Model.ID == -1))
            {
                <div style="margin-top:4px;">
                    @Html.LabelFor(m => m.UserName, new { @class = "labels", style = "width:100px;" })<span class="field-validation-error"> * </span>
                    @Html.TextBoxFor(m => m.UserName, new { @autocomplete = "off", @class = "TextBoxContent", style = "width:200px", @placeholder = "" })
                    @Html.ValidationMessageFor(m => m.UserName, null, new { @class = "Validation_Error" })
                </div>
            }
            else
            {
                <div style="margin-top:4px;">
                    @Html.LabelFor(m => m.UserName, new { @class = "labels", style = "width:100px;" })<span class="field-validation-error"> * </span>
                    @Html.TextBoxFor(m => m.UserName, new { @autocomplete = "off", @class = "TextBoxContent", style = "width:200px", @placeholder = "", @disabled = "disabled" })
                </div>
            }
            <div style="margin-top:2px;">
                @Html.LabelFor(m => m.AppId, new { @class = "labels", style = "width:99px;" })<span class="field-validation-error"> * </span>
                @Html.DropDownListFor(m => m.AppId, new SelectList(ViewBag.lstApp, "value", "text", 0), new { @class = "SelectBoxContent", style = "width: 200px" })
                @Html.ValidationMessageFor(m => m.AppId, null, new { @class = "Validation_Error" })
            </div>

            @if ((Model != null) && (Model.ID == -1))
            {
                <div style="margin-top:4px;">
                    @Html.LabelFor(m => m.Email, new { @class = "labels", style = "width:101px;" })<span class="field-validation-error"> * </span>
                    @Html.TextBoxFor(m => m.Email, new { @autocomplete = "off", @class = "TextBoxContent", style = "width:200px", @placeholder = "" })
                    @Html.ValidationMessageFor(m => m.Email, null, new { @class = "Validation_Error" })
                </div>
            }
            else
            {
                <div style="margin-top:4px;">
                    @Html.LabelFor(m => m.Email, new { @class = "labels", style = "width:101px;" })<span class="field-validation-error"> * </span>
                    @Html.TextBoxFor(m => m.Email, new { @autocomplete = "off", @class = "TextBoxContent", style = "width:200px", @placeholder = "", @disabled = "disabled" })
                    @Html.ValidationMessageFor(m => m.Email, null, new { @class = "Validation_Error" })
                </div>
            }
        </div>
    </div>
}


<div id="overlay" class="web_dialog_overlay"></div>
<div id="result" style="display: none; color:white;"></div>
@Scripts.Render("~/bundles/jqueryval")

<script>
    @functions{
            public string GetAntiForgeryToken()
            {
                string cookieToken, formToken;
                AntiForgery.GetTokens(null, out cookieToken, out formToken);
                return cookieToken + "," + formToken;
            }
        }
</script>
<script language="javascript">

    var $userForm = $("#user-form");
    var userExist = 'isExist';
    
    function save() {
        var result = false;
        if (getCheckUserExistFlag() == userExist) {
            checkUserExist();
        }        
        if ($userForm.valid() && userValid()) {
            //Neu chua ton tai Ung dung thi goi action them moi/cap nhat
            $.ajax({
                type: "POST",
                url: $userForm.attr('action'),
                data: $userForm.serialize(),
                dataType: '',//chú ý là form đặc biệt thì phải điền trường này
                async: false,
                headers: {
                    'VerificationToken': '@GetAntiForgeryToken()'
                },
                success: function (response) {
                    if (response != null && response.status == 200) {
                        window.notice(response.result, window.notice_success);
                        OnCriteria(crrPageIndex);
                        result = true;
                    } else {
                        window.notice(response.result, window.notice_error);
                    }
                }
            });
        }
        return result;
    }

    function userValid() {
        var result =  getCheckUserExistFlag();        
        if (result == userExist) {
            $("span[data-valmsg-for='UserName']").
                            html('<span class="field-validation-error" for="UserName">Tài khoản đã tồn tại</span>');
            return false;
        }

        return true;
    }

    function getCheckUserExistFlag(){
        var result = $('#result').html();

        return result;
    }

    $(document).ready(function () {
        $('#AppId').change(function () {
            checkUserExist();
        });

        $('.ui-icon ui-icon-closethick').bind('click', function () {
            alert(1);
            $('#user-model-container').dialog("destroy");
        });

        try {
            $("input[type='TextBoxContent']").each(function () {
                $(this).attr("autocomplete", "off");
            });
        }
        catch (e)
        { }
    });

    function checkUserExist() {
        $.ajax({
            type: "POST",
            url: '/Users/CheckExistUserName',
            data: {
                'UserName': $('#UserName').val(),
                'AppId': $('#AppId').val()
            },
            async: false,
            headers: {
                'VerificationToken': '@GetAntiForgeryToken()'
            },
            success: function (response) {
                if (response != true) {
                    $("span[data-valmsg-for='UserName']").
                        html('<span class="field-validation-error" for="UserName">Tài khoản đã tồn tại</span>');
                    $('#result').html(userExist);
                }
                else {
                    $("span[data-valmsg-for='UserName']").
                        html('');
                    $('#result').html('');
                }
            }
        });       
    }

</script>